/**
 * @Author YuanYuan
 * @Date 2022/8/2
 * @Time 13:12
 */


public class LinkedList {
    static class LinkNode {
        int val;
        LinkNode prev;
        LinkNode next;
        public LinkNode(int val) {
            this.val = val;
        }
    }
    LinkNode head = null;
    LinkNode tail = null;
    //头插法
    public void addFirst(int data){
        LinkNode node = new LinkNode(data);
        if(null == head) {
            node.prev = node;
            node.next = node;
            tail = node;
            head = node;
            return;
        }
        node.next = head;
        node.prev = head.prev;
        head.prev.next = node;
        head.prev = node;
        head = node;
    }
    //尾插法
    public void addLast(int data){
        LinkNode node = new LinkNode(data);
        if(null == head) {
            node.prev = node;
            node.next = node;
            tail = node;
            head = node;
            return;
        }
        node.prev = tail;
        node.next = head;
        tail.next = node;
        head.prev = node;
        tail = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data){
        LinkNode cur = head;
        if(index>size()||index<0) {
            return false;
        }
        if(0 == index) {
            addFirst(data);
            return true;
        }
        if(index == size()) {
            addLast(data);
            return true;
        }
        LinkNode node = new LinkNode(data);
        int num = index;
        while(num > 0) {
            num--;
            cur = cur.next;
        }
        node.prev = cur.prev;
        node.next = cur;
        cur.prev.next = node;
        cur.prev = node;
        return true;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        if(null == head) {
            return false;
        }
        LinkNode cur = head;
        while(cur!=tail) {
            if(key == cur.val) {
                return true;
            }
            cur = cur.next;
        }
        if(key == tail.val) {
            return true;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        LinkNode cur = head;
        if(null == head) {
            System.out.println("链表为空");
            return;
        }
        while(cur!=tail) {
            if(key == cur.val) {
                if(cur == head) {
                    head = cur.next;
                    return;
                }
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                return;
            }
            cur = cur.next;
        }
        if(key == tail.val) {
            tail = tail.prev;
            return;
        }
        System.out.println("没找到 删除失败");
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        boolean flag = false;
        LinkNode cur = head;
        if(null == head) {
            System.out.println("链表为空");
            return;
        }
        while(cur!=tail) {
            if(key == cur.val) {
                flag = true;
                if(cur == head) {
                    head = cur.next;

                }
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
            }
            cur = cur.next;
        }
        if(key == tail.val) {
            tail = tail.prev;
            return;
        }
        if(!flag) {
            System.out.println("没找到 删除失败");
        }

    }
    //得到单链表的长度
    public int size(){
        int size = 1;
        LinkNode cur = head;
        while (cur!=tail) {
            cur = cur.next;
            size++;
        }
        return size;
    }
    public void display() {
        LinkNode cur = head;
        if(null == cur) {
            System.out.println("链表为空");
            return;
        }
        if(cur.next == head) {
            System.out.println(head.val);
            return;
        }
        while(cur!=tail) {
            System.out.print(cur.val+ " ");
            cur = cur.next;
        }
        System.out.println(cur.val);
    }
    public void clear() {
        LinkNode cur = head;
        while(cur!=tail) {
            cur = cur.next;
            cur.prev = null;
        }
        tail = null;
        head = null;
        return;
    }
}
